package com.jxl.springboot.game.config.mvc;

import com.jxl.springboot.game.interceptor.SysHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * mvc配置类
 * @author 江雪林
 * @create 2021-04-22
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 这里使用 注入 SysHandlerInterceptor原因是：
     *  在SysHandlerInterceptor拦截器中需要使用 redis;
     */
    @Autowired
    private SysHandlerInterceptor sysHandlerInterceptor;

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(sysHandlerInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns("/login","/user/token","/user/login","/register","/user/register",
//                        "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg",
//                        "/**/*.jpeg", "/**/*.gif", "/**/*.ico","/error");
    }

    /**
     * 加载静态资源文件
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 控制器视图映射
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login");
        registry.addViewController("/main");
    }

    /**
     * 使用 default 方式将请求交于 tomcat 容器处理
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
